package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.service.IMobileStaffPermissionService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 员工权限 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-06-26
 */
@RestController
@RequestMapping("/api/mobileStaffPermission")
public class MobileStaffPermissionController {
    @Autowired
    IMobileStaffPermissionService iMobileStaffPermissionService;

    @ApiOperation("查询个人权限")
    @GetMapping("/{staffId}")
    public Result getStaffPermissionByStaffId(@PathVariable(value = "staffId") String staffId) {

        List<Long> staffPermissionList = iMobileStaffPermissionService.getStaffPermissionAllByStaffId(staffId);

        return new Result(Code.GET_OK, staffPermissionList, Code.GET_OK_MSG);
    }

    @ApiOperation("通过角色查询个人权限")
    @GetMapping("")
    public Result getStaffPermissionByStaffId(@RequestParam(value = "staffId", required = false) String staffId,
                                              @RequestParam(value = "roleId", required = false) String roleId) {
        if (StringUtils.isBlank(roleId)) {
            return new Result(Code.GET_OK, null, Code.GET_OK_MSG);
        }

        // 查找个人权限和角色权限，如果角色权限和个人权限冲突，以个人权限为准
        List<Long> staffPermissionList = iMobileStaffPermissionService.getStaffPermissionAll(staffId, roleId);

        return new Result(Code.GET_OK, staffPermissionList, Code.GET_OK_MSG);
    }


    @ApiOperation("个人权限新增/修改测试接口")
    @PutMapping("")
    public Result saveStaffPermissionByStaffId(@RequestBody Map reMap) {
        List<Long> staffPermissionList = (List<Long>) reMap.get("staffPermissionList");
        String staffId = (String) reMap.get("staffId");

        Boolean rs = iMobileStaffPermissionService.updateStaffPermissionByStaffId(staffId, staffPermissionList);

        return new Result(rs ? Code.OK : Code.ERROR);
    }
}
